/**
  * Created by Joshua Conero(O'Uymas) on 2016/10/17 0017.
  * QQ: 957376074
  * E-mail: Brximl@163.com
  * Scala语言学习于实践
  */
import java.io.{File, PrintWriter}
import java.sql.DriverManager
import scala.xml.XML
object Start {
  val helper = new Helper
  val about = new About
  // 系统开始运行时间
  val systemBegin = helper.detMsec()
  val config = "./config/config.xml"
  val xconf = XML.loadFile(config)
  // sqlite 通用对象
  Class.forName("org.sqlite.JDBC")
  val db = DriverManager.getConnection("jdbc:sqlite:Conero.db")
  // main 线程
  def main(args: Array[String]):Unit =  {
    val csl = xconf\"console"\"@name"
    val strict = (xconf\"console"\"@strict").toString().toLowerCase
    val cslName = csl.toString().toLowerCase()
    val len = args.length
    // 程序模式判断-> 配置文件处理加载/控制台参数
    var mode = "onlyConfig"
    if(len>0) mode = "onlyConsole"
    // 配置文件程序控制
    if (cslName != "" && cslName != "cmdexe" && cslName != "none" && mode == "onlyConfig"){
      cslName match {
        case "learn" => new Learn()
        case "crawler" => crawlerBegin()
      }
      return
    }
    if(mode == "onlyConfig" && (strict == "strict" || strict == "true")){
      println(helper.sysdate()+"(:= 配置-config.config文件无效，或者控制台没有任何参数输入")
      return
    }
    //new console(config).run(args)
    //println(helper.getFname("D:\\Joshua\\Active\\Scala\\app\\Scala_start\\runtime\\xlpu.xml",true))
    // 人为测试
    new console(config).run(Array("-crl","d:/Joshua/Active/scala/app/scala_start/runtime/xlpu.xml"))
    compileReport()
  }
  // 爬虫测试器
  def crawlerTest():Unit = {
    //this.test()
    var url = "http://127.0.0.1/Doeeking/"
    url = "http://www.gzsggzyjyzx.cn/list?cls=2&type=21"
    url = "http://www.ggzy.anshun.gov.cn/"
    url = "https://github.com/"
    url = "http://www.enterprisedb.com/products-services-training/pgdownload#windows"
    url = "http://127.0.0.1:9992/TeCenter/"
    //url = "http://127.0.0.1/_joshua/sysfile/guest.php"
    url = "http://127.0.0.1/Doeeking/"
    url = "http://www.swoole.com/"
    url = "http://www.gzsggzyjyzx.cn/"
    url = "http://www.gzsggzyjyzx.cn:8888/prt050a/auth/login"
    url = "https://www.taobao.com/"
    url = "https://www.jd.com/"
    url = "https://chaoshi.jd.com/"
    url = "https://www.baidu.com/"
    url = "http://www.yhd.com/"
    url = "http://www.oschina.net/"
    url = "https://www.oschina.net/project"
    url = "http://weibo.com/"
    url = "http://www.gov.cn/"
    url = "http://gy.fang.anjuke.com/"  // 贵阳安居可-购房网站


    val c = new Crawler(url)
    c.getBody()
    //c.getHtml()
    //c.sendData()
  }
  // 服务器-netty 包测试
  def nettyTest():Unit = {
    new Inet().run()
  }
  def Anjuke():Unit = {
    (new Crawler("")).anjuke()
  }
  // 幽灵爬虫入口
  def crawlerBegin():Unit = {
    val action = (xconf\"crawler"\"@action").toString()
    (new Crawler("")).ghost(config,action)
  }
  // 编译记录
  def compileReport():Unit = {
    val cpl = xconf\"app"\"compile"
    val times = (cpl\"@times").toString()
    val logs = (cpl\"@logs").toString()
    val str = (new Crypto).decode(helper.get_content(times).trim,about.name)
    val ctt = helper.str2int(str)+1
    val report = "【"+helper.sysdate()+"】编译/运行程序,耗时【"+helper.detMsec(systemBegin)+"s】,次数总计【"+ctt.toString+"】次\r\n"
    helper.put_content(times,(new Crypto).encode(ctt.toString,about.name))
    helper.file_append(logs,report)
    try {
      val sql = "insert into \"compile\"(\"etime\",\"misc\",\"times\") values('" + helper.sysdate() + "','" + helper.detMsec(systemBegin) + "','" + ctt + "')"
      db.createStatement().executeUpdate(sql)
    }catch{
      case e:Exception => {
        println("sqlite 数据库加载失败!!"+e.getMessage)
      }
    }
  }
}